package 栈;


import 动态数组.NewClassArray;

public class ArrayStack<E> implements Stack<E>{
    private NewClassArray<E> classArray;
    public ArrayStack(int capacity){
        this.classArray = new NewClassArray<>(capacity);
    }
    public ArrayStack() {
        this.classArray = new NewClassArray<>();
    }
    @Override
    public void push(E e) {
        this.classArray.addLast(e);
    }
    @Override
    public E pop() {
        return classArray.deleteLast();
    }
    @Override
    public E peek() {
        return classArray.getLast();
    }
    @Override
    public int getSize() {
        return this.classArray.getSize();
    }

    @Override
    public boolean isEmpty() {
        return this.classArray.isEmpty();
    }
    public int getCapacity(){
        return this.classArray.getCapacity();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(String.format("stack: \n"));
        builder.append("[");
        for (int i = 0; i < this.getSize(); i++) {
            builder.append(classArray.get(i));
            if (i != this.getSize() - 1){
                builder.append(",");
            }
        }
        builder.append("]top");
        return builder.toString();
    }
}
